'use strict';

const Service = require('egg').Service;

class EmployeeService extends Service {
  // 获取企业员工列表
  async getEnterpriseEmployees(enterpriseId, { page, pageSize, keyword, phone, status, hireTimeStart, hireTimeEnd }) {
    const { ctx } = this;
    
    // 确保分页参数为数字
    const pageNum = parseInt(page) || 1;
    const pageSizeNum = parseInt(pageSize) || 10;
    
    const where = { enterpriseId };
    
    // 添加搜索条件
    if (keyword) {
      where.employeeName = {
        [ctx.app.Sequelize.Op.like]: `%${keyword}%`
      };
    }
    
    if (phone) {
      where.phone = {
        [ctx.app.Sequelize.Op.like]: `%${phone}%`
      };
    }
    
    if (status) {
      where.employeeStatus = status;
    }
    
    // 入职时间范围查询
    if (hireTimeStart && hireTimeEnd) {
      where.hireTime = {
        [ctx.app.Sequelize.Op.between]: [hireTimeStart, hireTimeEnd]
      };
    } else if (hireTimeStart) {
      where.hireTime = {
        [ctx.app.Sequelize.Op.gte]: hireTimeStart
      };
    } else if (hireTimeEnd) {
      where.hireTime = {
        [ctx.app.Sequelize.Op.lte]: hireTimeEnd
      };
    }

    const { count, rows } = await ctx.model.Employee.findAndCountAll({
      where,
      order: [['hireTime', 'DESC']],
      limit: pageSizeNum,
      offset: (pageNum - 1) * pageSizeNum,
      include: [{
        model: ctx.model.Enterprise,
        as: 'enterprise',
        attributes: ['enterpriseName']
      }]
    });

    return {
      employees: rows,
      pagination: {
        total: count,
        page: pageNum,
        pageSize: pageSizeNum
      }
    };
  }

  // 创建员工
  async createEmployee(employeeData) {
    const { ctx } = this;
    
    const employee = await ctx.model.Employee.create({
      ...employeeData,
      hireTime: new Date(employeeData.hireTime)
    });

    return employee;
  }

  // 更新员工
  async updateEmployee(employeeId, updateData) {
    const { ctx } = this;
    
    const employee = await ctx.model.Employee.findByPk(employeeId);
    if (!employee) {
      throw new Error('员工不存在');
    }

    await employee.update({
      ...updateData,
      hireTime: updateData.hireTime ? new Date(updateData.hireTime) : employee.hireTime
    });

    return employee;
  }

  // 删除员工
  async deleteEmployee(employeeId) {
    const { ctx } = this;
    
    const employee = await ctx.model.Employee.findByPk(employeeId);
    if (!employee) {
      throw new Error('员工不存在');
    }

    await employee.destroy();
    return true;
  }

  // 批量操作员工
  async batchOperateEmployees(employeeIds, operation) {
    const { ctx } = this;
    
    const employees = await ctx.model.Employee.findAll({
      where: {
        employeeId: {
          [ctx.app.Sequelize.Op.in]: employeeIds
        }
      }
    });

    if (employees.length !== employeeIds.length) {
      throw new Error('部分员工不存在');
    }

    switch (operation) {
      case 'delete':
        await ctx.model.Employee.destroy({
          where: {
            employeeId: {
              [ctx.app.Sequelize.Op.in]: employeeIds
            }
          }
        });
        break;
      case 'deactivate':
        await ctx.model.Employee.update(
          { employeeStatus: 'inactive' },
          {
            where: {
              employeeId: {
                [ctx.app.Sequelize.Op.in]: employeeIds
              }
            }
          }
        );
        break;
      case 'activate':
        await ctx.model.Employee.update(
          { employeeStatus: 'active' },
          {
            where: {
              employeeId: {
                [ctx.app.Sequelize.Op.in]: employeeIds
              }
            }
          }
        );
        break;
      default:
        throw new Error('不支持的操作类型');
    }

    return { success: true, affectedCount: employeeIds.length };
  }
}

module.exports = EmployeeService;














